package com.goddess.yeecolor.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.goddess.yeecolor.Modle.Comment;
import com.goddess.yeecolor.Modle.Person;
import com.goddess.yeecolor.Modle.basic;
import com.goddess.yeecolor.Modle.user_info;
import com.goddess.yeecolor.R;
import com.goddess.yeecolor.interfaceAll.OnClickListener;
import com.goddess.yeecolor.tools.ImageUtils;
import com.goddess.yeecolor.view.CircleImageView;
import com.goddess.yeecolor.view.LoadingDialog;
import com.goddess.yeecolor.view.MyListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 评论主题
 */
public class Item_Exerciseitem_commitAdapter  extends BaseRecyclerAdapter<Item_Exerciseitem_commitAdapter.SimpleAdapterViewHolder> {
    private View.OnClickListener zanOnclick;
    private OnClickListener onClickListener;
    private LayoutInflater inflater;
    private SimpleAdapterViewHolder viewHolderAdapter;
    private Context context;
    private View.OnClickListener replyToCommentListener;
    private View.OnClickListener replyToReplyListener;
    private List<Comment> commentList;
    private Activity mActivity;
    private item_Exerciseitem_commit_contentAdapter commitContentAdapter;
    private boolean isshow;//删除是否显示
    public Item_Exerciseitem_commitAdapter(Context context, Activity activity, List<Comment> commentList,
                                           View.OnClickListener replyToCommentListener,
                                           item_Exerciseitem_commit_contentAdapter myAdapter, View.OnClickListener replyToReplyListener, boolean isshow) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.commentList = new ArrayList<Comment>();
        this.commentList.addAll(commentList);
        this.replyToCommentListener = replyToCommentListener;
        this.replyToReplyListener = replyToReplyListener;
        this.isshow=isshow;
        this.mActivity=activity;
    }
    @Override
    public SimpleAdapterViewHolder getViewHolder(View view) {
        return new SimpleAdapterViewHolder(view, false);
    }


    @Override
    public SimpleAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View view=LayoutInflater.from(context).inflate(R.layout.item_comment, parent,false);
        SimpleAdapterViewHolder viewHolder=new SimpleAdapterViewHolder(view,true);
        return viewHolder;
    }



    @Override
    public void onBindViewHolder(SimpleAdapterViewHolder holder, int position, boolean isItem) {
        Comment comment = commentList.get(position);
        user_info user_info=comment.getInfo();
        basic basic=user_info.getBasic();
        ImageUtils.getInstance().ImageStart(context,basic.getThumb(),holder.iv_user_photo);
        holder.tv_user_name.setText(basic.getNickname());
        holder.tv_user_comment.setText(comment.getContent());
        // 设置评论列表的点击效果透明
        holder.lv_user_comment_replys.setSelector(new ColorDrawable(
                Color.TRANSPARENT));
        holder.lv_user_comment_replys2.setSelector(new ColorDrawable(
                Color.TRANSPARENT));
        holder.tv_user_time.setText(comment.getTime());
        holder.tv_user_praise.setText(comment.getZan()+"");
        holder.tv_user_praise.setTag(position);
        holder.btn_comment_reply.setText(comment.getComment());
        /**
         * 点赞
         */
        holder.tv_user_praise.setTag(position);
        holder.tv_user_praise.setOnClickListener((view )->  {
            /**
             * 执行网络请求  更新UI  通过接口实现类似Item单行点击
             */
            int tag=holder.getLayoutPosition();
            onClickListener.OnClick(view,tag);


        });

        if (isshow){//判断是否有删除按钮
            holder.tv_user_comment_detele.setVisibility(View.VISIBLE);
            holder.lv_user_comment_replys2.setVisibility(View.VISIBLE);
            /**
             * 删除
             */
            holder.tv_user_comment_detele.setOnClickListener((view)->{
                LoadingDialog dialog=new LoadingDialog(context,mActivity,"确认删除",2);
                dialog.setCanceledOnTouchOutside(true);

                dialog.show();
                dialog.call.setText("确认");
                dialog.call.setOnClickListener((view1)->{
                    dialog.dismiss();
                    commentList.clear();
                    notifyDataSetChanged();
                });
            });
            //判断当前评论是否有回复
            if (commentList.get(position).getCommentses()!=null&&!commentList.get(position).getComment().equals("0")){

                commitContentAdapter=new item_Exerciseitem_commit_contentAdapter(context, commentList
                        .get(position).getCommentses(), position,
                        replyToReplyListener,isshow);
                holder.lv_user_comment_replys.setAdapter(commitContentAdapter);
            }
            //记录点击回复按钮时对应的position,用于确定所回复的对象
            holder.btn_comment_reply.setTag(position);
            holder.btn_comment_reply.setOnClickListener(replyToCommentListener);

        }
        else {
            holder.lv_user_comment_replys2.setVisibility(View.GONE);
            //判断当前评论是否有回复
            if (commentList.get(position).getCommentses()!=null&&commentList.get(position).getCommentses().size()!=0){
                commitContentAdapter=new item_Exerciseitem_commit_contentAdapter(context, commentList
                        .get(position).getCommentses(), position,
                        replyToReplyListener,isshow);
                holder.lv_user_comment_replys.setAdapter(commitContentAdapter);
            }
            else {
            }
            //记录点击回复按钮时对应的position,用于确定所回复的对象
            holder.btn_comment_reply.setTag(position);
            holder.btn_comment_reply.setOnClickListener(replyToCommentListener);
        }
    }
    @Override
    public int getAdapterItemCount() {
        return commentList.size();
    }

    public class SimpleAdapterViewHolder extends RecyclerView.ViewHolder {
        private CircleImageView iv_user_photo; // 评论者 头像
        private TextView tv_user_name; // 评论者 昵称
        private TextView tv_user_comment; // 评论者 一级品论内容
        private LinearLayout commit_linear;
        private TextView btn_comment_reply; // 评论者 二级评论按钮
        private MyListView lv_user_comment_replys,lv_user_comment_replys2; // 评论者 二级品论内容列表
        private TextView tv_user_praise;
        private TextView tv_user_time,tv_user_comment_detele;

        public SimpleAdapterViewHolder(View itemView, boolean b) {
            super(itemView);
            if (b) {
                tv_user_comment_detele= (TextView) itemView.findViewById(R.id.tv_user_comment_detele);
                iv_user_photo = (CircleImageView) itemView
                        .findViewById(R.id.iv_user_photo);
                tv_user_name = (TextView) itemView
                        .findViewById(R.id.tv_user_name);
                tv_user_comment = (TextView) itemView
                        .findViewById(R.id.tv_user_comment);
                btn_comment_reply = (TextView) itemView
                        .findViewById(R.id.tv_user_reply);
                lv_user_comment_replys = (MyListView) itemView
                        .findViewById(R.id.lv_user_comment_replys);
                tv_user_time= (TextView) itemView.findViewById(R.id.tv_user_tiem);
                tv_user_praise=(TextView)itemView.findViewById(R.id.tv_user_praise);
                commit_linear= (LinearLayout) itemView.findViewById(R.id.commit_linear);
                lv_user_comment_replys2= (MyListView) itemView.findViewById(R.id.lv_user_comment_replys2);
            }

        }
    }

    /**
     * 顶部添加数据
     * @param
     */
    public void setData(List<Person> lists) {

        commentList.clear();
        commentList.addAll(commentList);
        notifyDataSetChanged();
    }

    public void addData(List<Person>lists){
//        if (isClear){
//            list.clear();
//        }
        lists.addAll(lists);
        commentList.removeAll(commentList);
        commentList.addAll(commentList);
        notifyDataSetChanged();
    }
    public void insert(Comment person, int position) {
        insert(commentList, person, position);
    }

    public void remove(int position) {
        remove(commentList, position);
    }

    public void clear() {
        clear(commentList);
    }


    public void clearList() {
        this.commentList.clear();
    }
    public void iniser(Comment comment,int position){

        commentList.add(position,comment);
        notifyDataSetChanged();
    }
    public void updateList(List<Comment> commentList) {
        this.commentList.addAll(commentList);


    }
    public void setOnClickListener(OnClickListener onClickListener){
        this.onClickListener=onClickListener;
    }

    /**
     * 更新数据 先移除
     * @param position
     * @param comment
     */
    public void addData(int position,Comment comment) {
        commentList.add(position, comment);
        notifyItemInserted(position);
        notifyItemChanged(position); //更新
//        notifyItemRangeChanged(position,commentList.size()-position);

//
    }
    /**
     * 移除数据
     * @param position
     */
    public void removeData(int position) {
        commentList.remove(position);
        notifyItemRemoved(position);
//        notifyItemRangeChanged(position,commentList.size()-position);
    }

    public void insertData(int position,Comment comment){
        commentList.add(position,comment);
        notifyItemChanged(position);
    }
}
